
package com.jf.cloud.multishop.service.impl;

import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.AuditStatus;
import com.jf.cloud.multishop.mapper.ShopCompanyMapper;
import com.jf.cloud.multishop.model.ShopCompany;
import com.jf.cloud.multishop.service.ShopAuditingService;
import com.jf.cloud.multishop.service.ShopCompanyService;
import com.jf.cloud.multishop.vo.ShopAuditingVO;
import com.jf.cloud.multishop.vo.ShopCompanyVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author zz
 * @Date 2021/4/25 15:45
 */
@Service
public class ShopCompanyServiceImpl implements ShopCompanyService {

    @Autowired
    private ShopCompanyMapper shopCompanyMapper;

    @Autowired
    private ShopAuditingService shopAuditingService;
    @Autowired
    private MapperFacade mapperFacade;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByShopId(ShopCompany shopCompany, Long shopId, Integer temp) {
        this.checkInfo(shopCompany, false);
        shopCompanyMapper.updateByShopId(shopCompany, shopId, temp);
    }

    @Override
    public void batchUpdateStatus(Long shopId, Integer status) {
        shopCompanyMapper.batchUpdateStatus(shopId, status);
    }

    @Override
    public ShopCompanyVO getById(Long shopCompanyId) {
        ShopCompany shopCompany = shopCompanyMapper.getByShopCompanyId(shopCompanyId);
        return mapperFacade.map(shopCompany, ShopCompanyVO.class);
    }

    @Override
    public ShopCompanyVO getShopCompanyByShopIdAndStatus(Long shopId, Integer status) {
        ShopCompanyVO shopCompanyVO = shopCompanyMapper.getShopCompanyByShopIdAndStatus(shopId, status);
        // 检查工商信息是否有更新记录,若存在更新记录则发请求 查询审核信息 (逻辑上或许可以修改为存在多条工商信息记录)
        if (Objects.nonNull(shopCompanyVO)){
            Integer recordStatus = shopCompanyMapper.getLatestRecord(shopId);
            if (!Objects.equals(AuditStatus.SUCCESSAUDIT.value(), recordStatus)) {
                shopCompanyVO.setEdit(true);
                shopCompanyVO.setNewStatus(recordStatus);
            }
        }
        return shopCompanyVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ShopCompany shopCompany, boolean isCreate) {
        this.checkInfo(shopCompany, isCreate);
        shopCompanyMapper.save(shopCompany);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ShopCompany shopCompany) {
        shopCompanyMapper.update(shopCompany);
    }

    @Override
    public Boolean checkCreditCode(String creditCode, Long shopId) {
        Long shopIdByCreditCode = shopCompanyMapper.getShopIdByCreditCode(creditCode);
        if (Objects.nonNull(shopIdByCreditCode)) {
            return !Objects.equals(shopId, shopIdByCreditCode);
        }
        return false;
    }

    @Override
    public void deleteByShopIdAndStatus(Long shopId, Integer status) {
        shopCompanyMapper.deleteByShopIdAndStatus(shopId, status);
    }

    @Override
    public void checkInfo(ShopCompany shopCompany, Boolean isCreate) {
        if (isCreate) {
            ShopCompanyVO oldCompany = shopCompanyMapper.getShopCompanyByShopIdAndStatus(shopCompany.getShopId(), AuditStatus.SUCCESSAUDIT.value());
            if (Objects.nonNull(oldCompany)) {
                throw new LuckException("店铺工商信息已存在,不能重复新增");
            }
        } else {
            ShopAuditingVO shopAuditingVO = shopAuditingService.getByshopId(AuthUserContext.get().getTenantId());
            if (Objects.nonNull(shopAuditingVO) && Objects.equals(shopAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
                throw new LuckException("店铺处于待审核状态，无法修改店铺信息");
            }
        }
        //TODO 不校验营业执照号重复
//        if (checkCreditCode(shopCompany.getCreditCode(), shopCompany.getShopId())) {
//            throw new LuckException("统一信用码重复");
//        }
        if (shopCompany.getEndTime() != null && !shopCompany.getStartTime().before(shopCompany.getEndTime())) {
            throw new LuckException("营业起始日期不能大于营业终止时间");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(ShopCompany shopCompany) {
        shopCompanyMapper.updateById(shopCompany);
    }

}
